﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YWTSSys.BLL;
using YWTSSys.Library;
using YWTSSys.Model;
using System.Configuration;
using System.Data;
using System.Text;
using System.IO;

namespace YWTSSys.Web.MachineSys
{
    /// <summary>
    /// sysAjaxDone 的摘要说明
    /// </summary>
    public class sysAjaxDone : IHttpHandler
    {
        public enum ajaxmethod
        {
            deletemenu,
            deleterole,
            deleteuser,
            disableduser,
            enableduser,            
            /// <summary>
            /// 更改我的个性化设置
            /// </summary>
            changemysetting,
            /// <summary>
            /// 删除地区
            /// </summary>
            deletecustomerarea,
            deletecustomergroup,
            /// <summary>
            /// 重置电话状态
            /// </summary>
            resettelstatus
          

        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string method = context.Request["ajaxmethod"];

            ajaxmethod xmethod = (ajaxmethod)Enum.Parse(typeof(ajaxmethod), method.Trim().ToLower());
            string temp="";
            switch (xmethod)
            {
                case ajaxmethod.deletemenu:
                    temp = DeleteMenu();
                    break;
                case ajaxmethod.deleterole:
                    temp = DeleteRole();
                    break;
                case ajaxmethod.deleteuser:
                    temp = DeleteUser();
                    break;
                case ajaxmethod.disableduser:
                    temp=disableduser();
                    break;
                case ajaxmethod.enableduser:
                    temp = enableduser();
                    break;     
                case ajaxmethod.deletecustomerarea:
                    temp = deletecustomerarea();
                    break;   
                case ajaxmethod.deletecustomergroup:
                    temp = deletecustomergroup();
                    break; 
                case ajaxmethod.resettelstatus:
                    temp = resettelstatus();
                    break;
            }

            context.Response.Write(temp);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        protected void AddDeleteLog(string DWZRef, object newobj)
        {
            SLogInfo obj = new SLogInfo();
            obj.LogType = "操作";
            obj.OpType = Permission.删除.ToString();
            obj.DWZRef = DWZRef;
            obj.RealName = OUserController.CurrentUser.RealName;
            obj.OpOldObj = newobj == null ? "" : newobj.SerializableToXml();
            SLogController.Add(obj);
        }

        #region 操作JSON
        public enum callbackType
        {
            forward,
            closeCurrent
        }
        /// <summary>
        /// 输入操作JOSN
        /// </summary>
        /// <param name="statusCode">200表示成功,300表示失败</param>
        /// <param name="message">操作提示消息</param>
        /// <param name="navTabId">服务器转回navTabId可以把那个navTab标记为reloadFlag=1, 下次切换到那个navTab时会重新载入内容</param>
        /// <param name="callbackType">callbackType如果是closeCurrent就会关闭当前tab,只有callbackType="forward"时需要forwardUrl值</param>
        /// <param name="forwardUrl">重新加的页面路径</param>
        /// <returns></returns>
        private string ResponseOpJson(string statusCode, string message, string navTabId, callbackType callbackType, string forwardUrl)
        {
           // string jsontemp = "{{\"statusCode\":\"200\",\"message\":\"操作成功\",\"navTabId\":\"\",\"callbackType\":\"forward\",\"forwardUrl\":\"sysbasic/omeunlist.aspx\"}}";
            string jsontemp = "{{\"statusCode\":\"{0}\",\"message\":\"{1}\",\"navTabId\":\"{2}\",\"callbackType\":\"{3}\",\"forwardUrl\":\"{4}\"}}";
            return string.Format(jsontemp, statusCode, message, navTabId, callbackType.ToString(), forwardUrl);
        }

        private string SuccessJson(string message, string navTabId, string forwardUrl)
        {
            return ResponseOpJson("200", message, navTabId, callbackType.forward, forwardUrl);
        }
        private string SuccessJson(string message, string forwardUrl)
        {
            return ResponseOpJson("200", message, "", callbackType.forward, forwardUrl);
        }
        private string SuccessJson(string message)
        {
            return ResponseOpJson("200", message, "", callbackType.closeCurrent,"");
        }

        private string ErrorJson(string message, string navTabId, string forwardUrl)
        {
            return ResponseOpJson("300", message, navTabId, callbackType.forward, forwardUrl);
        }
        private string ErrorJson(string message, string forwardUrl)
        {
            return ResponseOpJson("300", message, "", callbackType.forward, forwardUrl);
        }
        private string ErrorJson(string message)
        {
            return ResponseOpJson("300", message, "", callbackType.closeCurrent, "");
        }
        #endregion

        #region 页面参数获取
        protected T GetRequestValue<T>(string name)
        {
            return HttpContext.Current.Request.GetRequestValue<T>(name);
        }

        protected T GetRequestValue<T>(string name, bool useDefValue, T defValue)
        {
            return HttpContext.Current.Request.GetRequestValue<T>(name, useDefValue, defValue);
        }
        protected T GetRequestValue<T>(string name, T defValue)
        {
            return HttpContext.Current.Request.GetRequestValue<T>(name, true, defValue);
        }
        #endregion
        #region sysbasic 系统管理模块相关
        private string DeleteMenu()
        {
            AddDeleteLog("OMeunList", GetRequestValue<int>("menuid"));

            bool r=  OMenuController.DeleteOMenuByMenuId(GetRequestValue<int>("menuid"));
            if (r)
                return SuccessJson("已成功删除相关菜单", "SysBasic/OMeunList.aspx");
            else
                return ErrorJson("删除失败","SysBasic/OMeunList.aspx");
        }

        private string DeleteRole()
        {
            AddDeleteLog("ORoleList", GetRequestValue<int>("roleid"));

            bool r = ORoleController.Delete(GetRequestValue<int>("roleid"));
            if (r)
                return SuccessJson("已成功删除角色", "SysBasic/ORoleList.aspx");
            else
                return ErrorJson("删除角色失败", "SysBasic/ORoleList.aspx");
           
        }
        private string DeleteUser()
        {
            AddDeleteLog("OUserList", GetRequestValue<int>("userid"));

            bool r = OUserController.Delete(GetRequestValue<int>("userid"));
            if (r)
                return SuccessJson("已成功删除用户", "SysBasic/OUserList.aspx");
            else
                return ErrorJson("删除用户失败", "SysBasic/OUserList.aspx");
        }

        private string disableduser()
        {
            int UserId = GetRequestValue<int>("userid");
            bool r = OUserController.ChangeState(UserId,1);
            if (r)
                return SuccessJson("已成功禁用用户", "SysBasic/OUserList.aspx");
            else
                return ErrorJson("禁用用户失败", "SysBasic/OUserList.aspx");
        }

        private string enableduser()
        {
            int UserId = GetRequestValue<int>("userid");
            bool r = OUserController.ChangeState(UserId, 0);
            if (r)
                return SuccessJson("已成功启用用户", "SysBasic/OUserList.aspx");
            else
                return ErrorJson("启用用户失败", "SysBasic/OUserList.aspx");
        }
        #endregion 









        #region 自动完成相关
        /// <summary>
        /// 查询关键字
        /// </summary>
        protected string key
        {
            get
            {
                return GetRequestValue<string>("q", true, "");
            }
        }
        /// <summary>
        /// 获取记录的前几条
        /// </summary>
        protected int toprow
        {
            get
            {

                return GetRequestValue<int>("limit", true, 20);
            }
        }
         
        #endregion
        /// <summary>
        /// 更新我的个性化设置
        /// </summary>
        /// <returns></returns>
        private string changemysetting()
        {
            string[] mysettingkey = new string[] { "AutoHideMenu", "AutoPoppingAlarm", "MyNumPerPage" };
            string mysettingvalue = "";
            foreach (string key in mysettingkey)
            {
                mysettingvalue += (string.IsNullOrEmpty(mysettingvalue)?"":",")+ key + "|" + GetRequestValue<string>(key, "");
            }

            OUserController.ChangeMySetting(OUserController.CurrentUser.UserId, mysettingvalue);
            return "";
        }

        private string deletecustomerarea()
        {
          //  AddDeleteLog("OUserList", GetRequestValue<int>("userid"));

            bool r = CustomerGroupController.DeleteArea(GetRequestValue<string>("AreaName"));
            if (r)
                return SuccessJson("已成功删除", "YWTSWeb/gl/CustomerGroupMasterListaspx.aspx");
            else
                return ErrorJson("删除失败", "YWTSWeb/gl/CustomerGroupMasterListaspx.aspx");
        }

        private string deletecustomergroup()
        {
            //  AddDeleteLog("OUserList", GetRequestValue<int>("userid"));

            bool r = CustomerGroupController.DeleteGroup(GetRequestValue<string>("GroupName"));
            if (r)
                return SuccessJson("已成功删除", "YWTSWeb/gl/CustomerGroupMasterListaspx.aspx");
            else
                return ErrorJson("删除失败", "YWTSWeb/gl/CustomerGroupMasterListaspx.aspx");
        }

        private string resettelstatus()
        {
            //  AddDeleteLog("OUserList", GetRequestValue<int>("userid"));

            bool r = CustomerController.Customer_ChangeTelStatus(GetRequestValue<string>("GroupName"));
            if (r)
                return SuccessJson("已成功重置", "YWTSWeb/gl/CustomerGroupMasterListaspx.aspx");
            else
                return ErrorJson("重置失败", "YWTSWeb/gl/CustomerGroupMasterListaspx.aspx");
        }
    }
}